/**
 * Looker  新闻整合系统
 * Author  :solosky
 * File    :NewsDisposer.java
 * Date    :May 21, 2009
 * Lisence : Apache License 2.0 
 */
package net.looker.monitor.disposer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.looker.config.ConfigManager;
import net.looker.data.Media;
import net.looker.data.News;
import net.looker.data.Media.ETYPE;
import net.looker.util.Html;
import net.looker.util.Tag;

/**
 * 处理新闻
 * 替换链接，去除HTML标记，计算TAG等
 * @author solosky
 *
 */
public class NewsDisposer  extends Disposer
{
	/**
	 * 需处理的新闻
	 */
	private News news;
	
	/**
	 * 构造函数
	 * @param news
	 */
	public NewsDisposer(News news)
	{
		this.news = news;
	}

	
	/* (non-Javadoc)
	 * @see net.looker.monitor.disposer.Disposer#dispose()
	 */
	@Override
	public void dispose() 
	{
		disposeOriginal();
		disposeBrief();
		disposeIntro();
		disposeThumb();
		disposeMedia();
		disposeTag();
	}
	
	/**
	 * 替换掉新闻中的URL为本地URL
	 * 如./logo.gif 要替换为showMedia.php?mhash={mediaHash}
	 * 否则由于页面变化很多图片没法显示
	 */
	private void disposeOriginal()
	{
		Iterator<Media> it   = news.getMediaList().iterator();
		String      original = news.getContent();
		String localMediaUrl = ConfigManager.getInstance().getLookerConfig().get("media.localURL");
		Media          media = null;
		while( it.hasNext())
		{
			media = it.next();
			if(media.hasError())
				//如果出现了错误，就替换为原来的绝对URL
				original = original.replace(media.getOriginal(), media.getUrl());	
			else
				//如果没出现错误，替换为本地ＵＲＬ
				original = original.replace(media.getOriginal(), localMediaUrl.replace("{mediahash}", media.getMediaHash()));	
		}
		
		news.setOriginal(original);
	}
	
	/**
	 * 去掉需要不需要保留的标签
	 */
	private void disposeBrief()
	{
		//保留p,br,a,img三个标记，然后再去掉其他的所有标记
		//首先替换需保留的标记为的<>为[]
		String brief = news.getOriginal();

		brief = replaceString(brief,"<p.*?>(.*?)</p>", "[p]$1[/p]");
		brief = replaceString(brief,"<br.*?>", "[br /]");
		brief = replaceString(brief,"<img(.*?)>", "[img$1]");
		brief = replaceString(brief,"<a(.*?)>(.*?)</a>", "[a$1]$2[/a]");
		
		//去除其他标记
		brief = replaceString(brief,"<script(.[^(</script>)]*)</script>" , "");
		brief = replaceString(brief,"<.*?>", "");
		brief = replaceString(brief,"&nbsp;", " ");
		news.setBrief(brief);
	}
	
	/**
	 * 去除所有的HTML标记
	 */
	private void disposeIntro()
	{
		news.setIntro(Html.stripTags(news.getContent()));
	}
	
	/**
	 * 查找新闻缩略图
	 */
	private void disposeThumb()
	{
		Iterator<Media> it   = news.getMediaList().iterator();
		Media media = null;
		while(it.hasNext())
		{
			media =it.next();
			if(news.getThumb()==null && !media.hasError() && media.getType()==Media.ETYPE.i)
			{
				news.setThumb(media.getMediaHash());
				return;
			}	
		}
		news.setThumb("0");
	}
	
	/**
	 * 处理新闻附件类型
	 */
	private void disposeMedia()
	{	
		Iterator<Media> it   = news.getMediaList().iterator();
		int[] mediaMask      = new int[Media.ETYPE.values().length]; 
		int mediaCnt		 = 0;
		Media media = null;
		while(it.hasNext())
		{
			media =it.next();
			
			if(media.hasError())
				continue;
			
			mediaMask[media.getType().ordinal()]++;
			mediaCnt++;
		}
		
		//设置附件统计
		news.setImage(mediaMask[Media.ETYPE.i.ordinal()]);
		news.setAudio(mediaMask[Media.ETYPE.a.ordinal()]);
		news.setVideo(mediaMask[Media.ETYPE.v.ordinal()]);
		news.setDoc(mediaMask[Media.ETYPE.d.ordinal()]);
		news.setZip(mediaMask[Media.ETYPE.z.ordinal()]);
		news.setOther(mediaMask[Media.ETYPE.o.ordinal()]);
		news.setMedia(mediaCnt);
		
	}
	
	/**
	 * 计算TAG
	 */
	private void disposeTag()
	{
		news.setTag(Tag.exactTag(news.getIntro()));
	}
	
	/**
	 * 替换字符
	 * String.replaceAll()是大小写敏感的，这里重写了下ReplaceALL主要让大小写不敏感
	 */
	private String replaceString(String source,String pattern,String replace)
	{
		return Pattern.compile(pattern,Pattern.CASE_INSENSITIVE).matcher(source).replaceAll(replace);
	}
	
	public static void main(String[] args)
	{
		News n = new News();
		Media m = new Media();
		m.setOriginal("/cdut/uploadfiles/uf1242874714424.zip");
		m.setUrl("http://www.cdut.edu.cn/cdut/uploadfiles/uf1242874714424.zip");
		//m.setExt("jpg");
		new MediaDisposer(m).dispose();
		System.out.println(m.getExt());
		n.getMediaList().add(m);
		NewsDisposer nd = new NewsDisposer(n);
		n.setContent("<html><head><TITLE>成都理工大学</TITLE><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><meta name=\"Author\" content=\"feng xianwen\"><link rel=\"stylesheet\" type=\"text/css\" href=\"/cdut/css/main.css\"/><script src=\"/cdut/js/common.js\"></script><script src=\"/cdut/js/navi.js\"></script></head><body leftmargin=20 topmargin=20 bgcolor=#eeeeee><table bgcolor=#666666 width=100% cellpadding=10 cellspacing=1 border=0><tr bgcolor=#ffffff><td><P class=MsoNormal style=\"MARGIN: 0cm -2.9pt 0pt 0cm; LINE-HEIGHT: 150%; TEXT-ALIGN: center\" align=center><B style=\"mso-bidi-font-weight: normal\"><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 150%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\"><?xml:namespace prefix = o ns = \"urn:schemas-microsoft-com:office:office\" /><o:p>&nbsp;</o:p></SPAN></B></P><P class=MsoNormal style=\"MARGIN: 0cm -2.9pt 0pt 0cm; LINE-HEIGHT: 150%; TEXT-ALIGN: center\" align=center><B style=\"mso-bidi-font-weight: normal\"><SPAN style=\"FONT-SIZE: 15pt; LINE-HEIGHT: 150%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">我校获“成都市第二届大学生普通物理知识竞赛”第四名<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></B></P><P class=MsoNormal style=\"MARGIN: 0cm -2.9pt 0pt 0cm; LINE-HEIGHT: 150%; TEXT-ALIGN: center\" align=center><B style=\"mso-bidi-font-weight: normal\"><SPAN lang=EN-US style=\"FONT-SIZE: 15pt; LINE-HEIGHT: 150%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\"><o:p>&nbsp;</o:p></SPAN></B></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><?xml:namespace prefix = st1 ns = \"urn:schemas-microsoft-com:office:smarttags\" /><st1:chsdate w:st=\"on\" IsROCDate=\"False\" IsLunarDate=\"False\" Day=\"16\" Month=\"5\" Year=\"2009\"><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">2009</SPAN><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">年<FONT size=3><FONT face=宋体><SPAN lang=EN-US>5</SPAN>月<SPAN lang=EN-US>16</SPAN>日</FONT></FONT></SPAN></st1:chsdate><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">，成都市第二届大学生普通物理知识竞赛决赛暨颁奖典礼在四川大学成功举行。<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><st1:chsdate w:st=\"on\" IsROCDate=\"False\" IsLunarDate=\"False\" Day=\"18\" Month=\"4\" Year=\"2009\"><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">2009</SPAN><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">年<FONT size=3><FONT face=宋体><SPAN lang=EN-US>4</SPAN>月<SPAN lang=EN-US>18</SPAN>日</FONT></FONT></SPAN></st1:chsdate><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">，成都市第二届大学生普通物理知识竞赛初赛同时在我校和四川大学，电子科技大学，西南交通大学，四川师范大学，西华大学，成都信息工程学院拉开战幕，共有来自各高校的近八千名学子积极参加了初赛。经过同学们激烈角逐和老师的精心筛选，上述七大高校各选出<FONT size=3><FONT face=宋体><SPAN lang=EN-US>3</SPAN>人组成代表队参加最终决赛。我校由信息工程学院物理系主任张正阶教授和物理系副主任赵晓凤老师等带队，<SPAN lang=EN-US>07</SPAN>级应用物理专业的陈楚翘，岳凌，李彬代表我校参赛。<SPAN lang=EN-US><o:p></o:p></SPAN></FONT></FONT></SPAN></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><st1:chsdate w:st=\"on\" IsROCDate=\"False\" IsLunarDate=\"False\" Day=\"16\" Month=\"5\" Year=\"2009\"><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">5</SPAN><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">月<FONT size=3><FONT face=宋体><SPAN lang=EN-US>16</SPAN>日</FONT></FONT></SPAN></st1:chsdate><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">下午<FONT size=3><FONT face=宋体><SPAN lang=EN-US>2</SPAN>：<SPAN lang=EN-US>30</SPAN>，四川大学艺术学院小剧场气氛热烈。四川大学教授高洁院士，成都市物理学会会长及各高校物理学教授出席了决赛。高洁院士深入浅出地为同学们讲解了很多物理学中有趣的研究成果，并鼓励同学们积极进行物理科学研究，为物理学的发展作出贡献。在场的同学都倍受鼓舞。<SPAN lang=EN-US><o:p></o:p></SPAN></FONT></FONT></SPAN></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; LINE-HEIGHT: 200%; TEXT-ALIGN: left\" align=left><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">决赛在精彩的文艺节目后正式拉开战幕。比赛采用知识竞答的形式，分为必答题、选答题和抢答题三环节。各环节题目既充满趣味又富于物理知识。例如“请用物理知识解释暖水瓶为何保温”等，寓教于乐。在激烈而紧张的比赛过程中，各代表队选手们快速反应、从容应答，频频赢得评委老师及观众的好评。经过约半小时的比赛。最终西南交通大学代表队以稳定的发挥摘得桂冠，我校代表队获得第四名，同时获得“最佳团队协作奖”。<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\"><o:p></o:p></SPAN></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">本届普通物理知识竞赛不仅为同学们提供了一个展现自我的平台，更充分地激发了同学们的科学研究精神，鼓舞了一大批有志于物理知识研究的同学，同时也加强了各高校之间的联系，可谓一举多得。<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">我校信息工程学院承办了理工大学赛区的比赛，承办方精心组织，同学们勇跃参与。因为有学院的大力支持，老师们的耐心指导，我校同学们取得了佳绩。</SPAN></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\"></SPAN>&nbsp;</P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\"><IMG id=uploadedFile alt=uf1242874714424.jpg src=\"/cdut/uploadfiles/uf1242874714424.jpg\" ufurl=\"/cdut/uploadfiles/uf1242874714424.jpg\"></SPAN></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\"></SPAN>&nbsp;</P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\"><SPAN style=\"FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA\"><STRONG>附：我校获奖情况</STRONG></SPAN></SPAN></P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\"></SPAN>&nbsp;</P><P class=MsoNormal style=\"MARGIN: 0cm 6pt 0pt 0cm; TEXT-INDENT: 24pt; LINE-HEIGHT: 200%; TEXT-ALIGN: left; mso-char-indent-count: 2.0\" align=left><SPAN style=\"FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 宋体; mso-ascii-theme-font: minor-fareast; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\"><A id=uploadedFile href=\"/cdut/downloadServlet?url=/cdut/uploadfiles/uf1242874916307.doc&amp;label=uf1242874916307.doc\" target=_blank ufurl=\"/cdut/uploadfiles/uf1242874916307.doc\" alt=\"uf1242874916307.doc\"><IMG src=\"/cdutadmin/editor/images/doc.gif\" border=0>请点击此处下载</A><SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P></td></tr></table><br><br><br><table width=100% cellpadding=0 cellspacing=0 border=0><tr><td><P>学校地址：成都市成华区二仙桥东三路1号 邮政编码：610059 传真：028-84077163<BR>成都理工大学网络教育学院网络信息中心&nbsp;© 2004<BR><FONT size=2>蜀ICP备05026980号</FONT></P></td><td align=right valign=middle>由<font color=blue>宣传部</font>创建于2009/05/21　共访问 855 次　　[ <a class=news href=\"#\" onclick=\"window.close();\">关闭窗口</a> ]</td></tr></table></body></html>");
		nd.dispose();
		//System.out.println(nd.replaceString(n.getContent(), "<(/?)font.*?>", "[$1font]"));
		//System.out.println(n.getContent().replace("<span.*?>", "[span]"));
		System.out.println(n.getBrief());
		System.out.println(n.getIntro());
		System.out.println(n.getMedia());
		System.out.println(n.getThumb());
		System.out.println(n.getTag());
	}

}
